/* Bootstrap_extensions.do

Note: Requires "ssc inst _gwtmean" for some code. Also, the thing is riddled 
with errors and often doesn't run succesfully. Beats me why.

USING Define_Extensions.do as instructions.

Creates bootstrapped standard errors for all the columns in the formatted
Aguiar and bils replication tables.

One repetition of the bootstrap takes about 100 seconds on the ND CRC, comes out 
to about 2.75 hours for 100 repetitions

Note that the current definition of second_stage_all calculates the DIFFERENCES
in log inequality for all exccept the first period of time. The other 
definitions calculate levels. (The difference conmes from the option "noomit" in
the construction of DdecX vars)

*/

clear all
set more off
ssc inst _gwtmean
capture ssc inst estout
mata mata mlib index

set matsize 10000

cd "$path_name"

version 9

qui do Define_Extensions.do
//qui do benchmark_define_crc.do
//second_stage_benchmark
// This also loads up the proper data file.

timer clear
// Switch this on to run the column thing...
second_stage_dataset
// Define globals for columns of import:

global v6 sheltr fdhomer vehpchr oppubr utilr healthr equpmtr fdawayr tvrdotr adultclr feeardr cashcor furnshr

// And more globals for the bootstrap
	global blist
	foreach spec in OLS{
		foreach num of numlist 1/9 {
			foreach num2 of numlist 3 5 {
				global blist $blist  `spec'b_`num'_`num2' = r(`spec'b_`num'_`num2')
			}
		}
	}
	foreach num in 3 5{
		foreach p in 4to6 6to8 8to9{
			global blist $blist IneqChange_`num'_`p' = r(IneqChange_`num'_`p')
		}
	}	

	global blist $blist PanB1 = r(PanB1) PanB4 = r(PanB4) PanB4to6 = r(PanB4to6) ///
	PanB6to8 = r(PanB6to8) PanB8to9 = r(PanB8to9) PanB1to8 = r(PanB1to8) PanB1to9 = r(PanB1to9)

	global blist $blist
	foreach rat in Hi_v_Low Med_v_Low Hi_v_Med{
		foreach p in 1 4_to_1 6_to_4 8_to_6 9_to_8 8_to_1 9_to_1{
			global blist $blist logch_resid_`rat'_`p' = r(logch_resid_`rat'_`p')
		}
	}

********************************************************************************	
// Column 1 (Paper - uses all cats)
timer clear
matrix drop _all
timer on 1
bs $blist, reps(100) seed(1) cluster(id): second_stage_all
timer off 1
timer list

estout using "Excel/bs_col1.xls", cells(b se) stats(N_clust N_reps) replace

********************************************************************************
// Column 2 (Well-measured)
timer clear
matrix drop _all
timer on 1
bs $blist, reps(100) seed(1): second_stage_all ///
sheltr fdhomer vehpchr oppubr utilr equpmtr tvrdotr
timer off 1
timer list

estout using "Excel/bs_col2.xls", cells(b se) stats(N_clust N_reps) replace

********************************************************************************
// Column 3 (< 5% of obs are 0)
timer clear
matrix drop _all
timer on 1
bs $blist, reps(100) seed(1): second_stage_all ///
sheltr fdhomer vehpchr oppubr utilr equpmtr fdawayr tvrdotr 
timer off 1
timer list

estout using "Excel/bs_col3.xls", cells(b se) stats(N_clust N_reps) replace

********************************************************************************
// Column 4 (< 10% of obs are 0)
timer clear
matrix drop _all
timer on 1
bs $blist, reps(100) seed(1) cluster(id): second_stage_all ///
sheltr fdhomer vehpchr healthr oppubr utilr equpmtr fdawayr tvrdotr adultclr
timer off 1
timer list

estout using "Excel/bs_col4.xls", cells(b se) stats(N_clust N_reps) replace

********************************************************************************
// Column 5 (Consumption Share > 2%)
timer clear
matrix drop _all
timer on 1
bs $blist, reps(100) seed(1) cluster(id): second_stage_all ///
sheltr fdhomer vehpchr oppubr utilr healthr equpmtr fdawayr tvrdotr adultclr ///
feeardr cashcor
timer off 1
timer list

estout using "Excel/bs_col5.xls", cells(b se) stats(N_clust N_reps) replace

********************************************************************************
// Column 6 (Thirteen largest categories)
timer clear
timer on 1
bs $blist, reps(100) seed(1) cluster(id): second_stage_all $v6
timer off 1
timer list

estout using "Excel/bs_col6.xls", cells(b se) stats(N_clust N_reps) replace

********************************************************************************
// Column 7 (6 + Domestic Services and Childcare)
timer clear
matrix drop _all
timer on 1
bs $blist, reps(100) seed(1) cluster(id): second_stage_all $v6 housopr
timer off 1
timer list

estout using "Excel/bs_col7.xls", cells(b se) stats(N_clust N_reps) replace

********************************************************************************
// Column 8 (7 + Education)
timer clear
matrix drop _all
timer on 1
bs $blist, reps(100) seed(1) cluster(id): second_stage_all $v6 ///
housopr educar
timer off 1
timer list

estout using "Excel/bs_col8.xls", cells(b se) stats(N_clust N_reps) replace


